package com.chen.pfm.slice;

import com.chen.pfm.AAChartCoreLib.AAChartCreator.AAChartModel;
import com.chen.pfm.AAChartCoreLib.AAChartCreator.AAChartView;
import com.chen.pfm.ResourceTable;
import com.chen.pfm.entity.ChartData;
import com.chen.pfm.entity.Classify;
import com.chen.pfm.entity.ReportForms;
import com.chen.pfm.event.MyEventHandler;
import com.chen.pfm.provider.MypageProvider;
import com.chen.pfm.provider.ReportFormProvider;
import com.chen.pfm.provider.TabPageSliderProvider;
import com.chen.pfm.provider.TitleProvider;
import com.chen.pfm.utils.AAChartUtil;
import com.chen.pfm.utils.ClassifyDBUtil;
import com.chen.pfm.utils.DateUtils;
import com.chen.pfm.utils.MyToast;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.dialog.ListDialog;
import ohos.app.AbilityContext;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.List;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;

public class GraphicStatisticsAbilitySlice extends AbilitySlice {
    final static HiLogLabel
            LABEL = new HiLogLabel(HiLog.LOG_APP, 0x2022003, "GraphicStatisticsAbilitySlice");
    TabList statisticsTabList;//统计页的导航栏
    PageSlider statisticsPageSlider;//页面切换
    List<ChartData> pieDateExpenditurePastSevenDays;//圆饼图近七日
    List<ChartData> pieDateIncomePastSevenDays;
    List<ChartData> pieDatepropertyPastSevenDays;
    List<ChartData> pieDateExpenditureByMonth;//圆饼图本月
    List<ChartData> pieDateIncomeByMonth;
    List<ChartData> pieDatepropertyByMonth;
    List<ReportForms> monthReportFormsIncome;
    List<ReportForms> monthReportFormsExpenditure;
    List<ReportForms> monthReportFormsProperty;
    List<ReportForms> yearReportFormsIncome;
    List<ReportForms> yearReportFormsExpenditure;
    List<ReportForms> yearReportFormsProperty;
    List<ChartData> pieDateExpenditureByYear;//圆饼图本年
    List<ChartData> pieDateIncomeByYear;
    List<ChartData> pieDatepropertyByYear;
    //视图
    AAChartView AAChartView_pie;
    AAChartView AAChartView_line;
    AAChartView AAChartView_column;
    //统计页的选择类型文本框
    Text select_type;
    Text select_year;
    Text select_yearmonth;
    Text displayReportForms_ByMoth;
    Text displayReportForms_ByYear;
    //统计类型  近七天/本月/年
    int statisticalType = 0;
    String statisticalTime;
    int classifType = 0;
    //选择器的索引值
    int selectYearIndex;//年
    int selectMonthIndex;//月
    ReportFormProvider reportFormProvider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_statistics);
        //初始化数据
        initChartData();
        //展示
        initStatisticsTabListAndpageSlider();

        MyToast.showMyToast(this, "数据加载完成！");

        HiLog.info(LABEL, "GraphicStatisticsAbilitySlice开始");

    }

    private void initChartData() {
        //饼图
//        过去七日
        try {
            if (pieDateIncomePastSevenDays != null && pieDateIncomePastSevenDays != null && pieDatepropertyPastSevenDays != null) {
                MyToast.showMyToast(this, "数据加载成功,正在渲染");
            } else {
                pieDateExpenditurePastSevenDays = ClassifyDBUtil.statisticalMoneyPastSevenDays(this, 1);
                pieDateIncomePastSevenDays = ClassifyDBUtil.statisticalMoneyPastSevenDays(this, 2);
                pieDatepropertyPastSevenDays = ClassifyDBUtil.statisticalMoneyPastSevenDays(this, 3);
            }

        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        //本月
        String strCurrentYearandMonth = DateUtils.getStrCurrentYearandMonth();
        try {

            if (pieDateIncomeByMonth != null && pieDateIncomeByMonth != null && pieDatepropertyByMonth != null) {
                MyToast.showMyToast(this, "数据加载成功,正在渲染");
            } else {
                pieDateExpenditureByMonth = ClassifyDBUtil.statisticalMoneyCurrentMonthOrYear(this, strCurrentYearandMonth, 1);
                pieDateIncomeByMonth = ClassifyDBUtil.statisticalMoneyCurrentMonthOrYear(this, strCurrentYearandMonth, 2);
                pieDatepropertyByMonth = ClassifyDBUtil.statisticalMoneyCurrentMonthOrYear(this, strCurrentYearandMonth, 3);
                monthReportFormsIncome = ClassifyDBUtil.statisticsReportByRtimeAndType1(this, strCurrentYearandMonth, 2);
                monthReportFormsExpenditure = ClassifyDBUtil.statisticsReportByRtimeAndType1(this, strCurrentYearandMonth, 1);
                monthReportFormsProperty = ClassifyDBUtil.statisticsReportByRtimeAndType1(this, strCurrentYearandMonth, 3);
                HiLog.error(LABEL, "monthReportFormsIncome=" + monthReportFormsIncome);
            }

        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        //本年
        try {
            if (pieDateIncomeByYear != null && pieDateIncomeByYear != null && pieDatepropertyByYear != null) {
                MyToast.showMyToast(this, "数据加载成功,正在渲染");
            } else {
                String strCurrentYear = DateUtils.getStrCurrentYear();
                pieDateExpenditureByYear = ClassifyDBUtil.statisticalMoneyCurrentMonthOrYear(this, strCurrentYear, 1);
                pieDateIncomeByYear = ClassifyDBUtil.statisticalMoneyCurrentMonthOrYear(this, strCurrentYear, 2);
                pieDatepropertyByYear = ClassifyDBUtil.statisticalMoneyCurrentMonthOrYear(this, strCurrentYear, 3);
                yearReportFormsIncome = ClassifyDBUtil.statisticsReportByRtimeAndType1(this, strCurrentYear, 2);
                yearReportFormsExpenditure = ClassifyDBUtil.statisticsReportByRtimeAndType1(this, strCurrentYear, 1);
                yearReportFormsProperty = ClassifyDBUtil.statisticsReportByRtimeAndType1(this, strCurrentYear, 3);

            }

        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
    }

    void initStatisticsTabListAndpageSlider() {
        HiLog.info(LABEL, "你来到了统计页");

        statisticsTabList = (TabList) findComponentById(ResourceTable.Id_statisticsTabList);
        statisticsPageSlider = (PageSlider) findComponentById(ResourceTable.Id_statisticsPageSlider);
        //加载统计页的导航栏
        String[] tabTexts = {"近七天", "月统计", "年统计"};
        for (int i = 0; i < tabTexts.length; i++) {
            TabList.Tab tab = statisticsTabList.new Tab(this);
            tab.setText(tabTexts[i]);
            statisticsTabList.addTab(tab);
        }
        //绑定导航栏的页面切换，反之也页面切换引起导航栏选择切换
        List<Integer> list = new ArrayList<>();
        list.add(ResourceTable.Layout_ability_statistics_pastSevenDays);
        list.add(ResourceTable.Layout_ability_statistics_month);
        list.add(ResourceTable.Layout_ability_statistics_year);

        MypageProvider mypageProvider = new MypageProvider(list, this);
        statisticsPageSlider.setProvider(mypageProvider);//设置Provider


        //导航栏和页面的联动
        statisticsTabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                int position = tab.getPosition();
                statisticalType = position;
                statisticsPageSlider.setCurrentPage(position);
                if (position == 0) {//近七天
                    statisticsBypastSevenDays();
                } else if (position == 1) {//月统计
                    classifType=0;
                    statisticsByMonth();
                } else if (position == 2) {//年统计
                    classifType=0;
                    statisticsByYear();
                }
            }

            @Override
            public void onUnselected(TabList.Tab tab) {

            }

            @Override
            public void onReselected(TabList.Tab tab) {

            }
        });
        statisticsPageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int i) {
                if (statisticsTabList.getSelectedTabIndex() != i) {
                    statisticsTabList.selectTabAt(i);
                }
            }
        });
        statisticsTabList.selectTabAt(0);
    }

    //本月统计绘图
    private void statisticsByMonth() {

        AAChartView_pie = (AAChartView) findComponentById(ResourceTable.Id_AAChartView_pieDateByMonth);
        AAChartView_column = (AAChartView) findComponentById(ResourceTable.Id_AAChartView_ColumnDateByMonth);
        AAChartView_line = (AAChartView) findComponentById(ResourceTable.Id_AAChartView_LineDateByMonth);
        select_type = (Text) findComponentById(ResourceTable.Id_select_type_ByMoth);
        select_type.setClickedListener(component -> showPickClassifyType(this, component));
        displayReportForms_ByMoth = (Text) findComponentById(ResourceTable.Id_displayReportForms_ByMoth);
        displayReportForms_ByMoth.setClickedListener(component -> showReportForms(this, component));
        select_yearmonth = (Text) findComponentById(ResourceTable.Id_select_yearmonth);
        select_yearmonth.setText(DateUtils.getStrCurrentYearandMonth());
        select_yearmonth.setClickedListener(component -> showSelectYearAndMonthList(select_yearmonth));

        HiLog.info(LABEL, "数据加载成功: ");

        //直方图
        drawColumnChartByYearMonth("本月统计", "走势如下", DateUtils.getStrCurrentYearandMonth());
        //折线图
        drawLineChartByYearMonth("本月统计", "走势如下", DateUtils.getStrCurrentYearandMonth());

        statisticalTime=DateUtils.getStrCurrentYearandMonth();

        handlingDisplay(0,statisticalTime);
    }

    //本年统计绘图
    private void statisticsByYear() {
        AAChartView_pie = (AAChartView) findComponentById(ResourceTable.Id_AAChartView_pieDateByYear);
        AAChartView_column = (AAChartView) findComponentById(ResourceTable.Id_AAChartView_ColumnDateByYear);
        AAChartView_line = (AAChartView) findComponentById(ResourceTable.Id_AAChartView_LineDateByYear);
        select_type = (Text) findComponentById(ResourceTable.Id_select_type_ByYear);
        select_type.setClickedListener(component -> showPickClassifyType(this, component));
        displayReportForms_ByYear = (Text) findComponentById(ResourceTable.Id_displayReportForms_ByYear);
        displayReportForms_ByYear.setClickedListener(component -> showReportForms(this, component));
        select_year = (Text) findComponentById(ResourceTable.Id_select_year);
        select_year.setText(DateUtils.getStrCurrentYear());
        select_year.setClickedListener(component -> showSelectYearList(select_year));
        HiLog.info(LABEL, "数据加载成功: ");

        //直方图
        drawColumnChartByYear("本年统计", "整体走势", DateUtils.getStrCurrentYear());
        //折线图
        drawLineChartByYear("本年统计", "整体走势", DateUtils.getStrCurrentYear());

        statisticalTime = DateUtils.getStrCurrentYear();
        //本年的报表和圆饼图
        handlingDisplay(0,statisticalTime);
    }

    //过去七天统计
    private void statisticsBypastSevenDays() {
        AAChartView_pie = (AAChartView) findComponentById(ResourceTable.Id_AAChartView_pieDate);
        AAChartView_column = (AAChartView) findComponentById(ResourceTable.Id_AAChartView_ColumnDate);
        AAChartView_line = (AAChartView) findComponentById(ResourceTable.Id_AAChartView_LineDate);
        select_type = (Text) findComponentById(ResourceTable.Id_select_type);
        select_type.setClickedListener(component -> showPickClassifyType(this, component));
        //页面的AA绘图显示组件

        //饼图
        handlingDisplay(0, null);

        //直方图
        drawColumnChartPastSevenDaysData("近七日收支统计", "过去七天");

        //折线图
        drawLineChartPastSevenDaysData("近七日收支统计", "过去七天");

    }

    public void showPickClassifyType(AbilityContext context, Component com) {
        //分类类型
        getUITaskDispatcher().asyncDispatch(() -> {
            ListDialog listDialog = new ListDialog(context, ListDialog.SINGLE);
            listDialog.setCornerRadius(45);
            listDialog.getProvider();
//            listDialog.setSize(800, 600);
            listDialog.setSize(800, MATCH_CONTENT);
            String[] strings = {"支出", "收入", "资产"};
            List<String> titles = new ArrayList<>();
            titles.add("支出");
            titles.add("收入");
            titles.add("资产");
            listDialog.setItems(strings);
            TitleProvider titleProvider = new TitleProvider(titles, this);
            listDialog.setProvider(titleProvider);
            listDialog.setButton(0, "取消", (iDialog, i) -> {
                iDialog.hide();
                MyToast.showMyToast(this, "已取消");

            });
            listDialog.setButton(1, "确定", (iDialog, i) -> {
                iDialog.hide();
                MyToast.showMyToast(this, "已确定");
            });
            //展示
            listDialog.show();
            //监听
            ((ListContainer) listDialog.getListContainer()).setItemSelectedListener((listContainer, component, i, l) -> {
                MyToast.showMyToast(this, "你选择了" + strings[i]);
                select_type.setText(strings[i]);
                classifType = i;
                handlingDisplay(i,statisticalTime);
                //改变渲染数据
                listDialog.hide();

            });
        });
    }

    void handlingDisplay(int i, String time) {
        String title = null;
        if (statisticalType == 0) {//近七天
            if (i == 0) {//支出
                drawPieChart("近七天统计", "支出", "方面总支出", pieDateExpenditurePastSevenDays);
            } else if (i == 1) {//收入
                drawPieChart("近七天统计", "收入", "方面收入", pieDateIncomePastSevenDays);
            } else {//资产
                drawPieChart("近七天统计", "资产", "方面", pieDatepropertyPastSevenDays);
            }

        } else if (statisticalType == 1) {//月统计
            if(time.equals(DateUtils.getStrCurrentYearandMonth())){
                title = "本月统计";
            }else {
                title = time+"统计";
            }
            //饼图
            if (i == 0) {//支出
                drawPieChart(title, "支出", "总支出为", pieDateExpenditureByMonth);
                reportFormProvider = new ReportFormProvider(monthReportFormsExpenditure, this);
            } else if (i == 1) {//收入
                drawPieChart(title, "收入", "总收入为", pieDateIncomeByMonth);
                reportFormProvider = new ReportFormProvider(monthReportFormsIncome, this);
            } else {//资产
                drawPieChart(title, "资产", "方面", pieDatepropertyByMonth);
                reportFormProvider = new ReportFormProvider(monthReportFormsProperty, this);
            }

        } else {//年统计
            if(time.equals(DateUtils.getStrCurrentYear())){
                title = "本年统计";
            }else {
                title = time+"统计";
            }

            //饼图
            if (i == 0) {//支出
                drawPieChart(title, "支出", "总支出为", pieDateExpenditureByYear);
                reportFormProvider = new ReportFormProvider(yearReportFormsExpenditure, this);

            } else if (i == 1) {//收入
                drawPieChart(title, "收入", "总收入为", pieDateIncomeByYear);
                reportFormProvider = new ReportFormProvider(yearReportFormsIncome, this);
            } else {//资产
                drawPieChart(title, "资产", "方面", pieDatepropertyByYear);
                reportFormProvider = new ReportFormProvider(yearReportFormsProperty, this);
            }


        }

    }

    void drawLineChartPastSevenDaysData(String title, String subtitle) {
//        getUITaskDispatcher().asyncDispatch(() -> {
        //得到近七天的时间x轴标签
        String[] name = DateUtils.getPastSevenDate();
        //数据转换
        Object[] income = ChartData.getPastSevenDaysData(this, 2);//2是收入
        Object[] expenditure = ChartData.getPastSevenDaysData(this, 1);//1是支出
        Object[] property = ChartData.getPastSevenDaysData(this, 3);
        Object[] surpuls = new Object[7];
        for (int i = 0; i < 7; i++) {
            surpuls[i] = (Double) income[i] - (Double) expenditure[i];
        }

        AAChartModel aaChartModel = AAChartUtil.configureAAChartLineModel(title, subtitle, name, income, expenditure, property, surpuls);
        /*图表视图对象调用图表模型对象,绘制最终图形*/
        AAChartView_line.aa_drawChartWithChartModel(aaChartModel);
//        });
    }

    void drawLineChartByYearMonth(String title, String subtitle, String yearmonth) {
//        getUITaskDispatcher().asyncDispatch(() -> {
        //得到月的日时间x轴标签
        String[] X_axis = DateUtils.getX_axisByMonth(yearmonth);
        int size = X_axis.length;
        //数据转换
        Object[] expenditure = ChartData.getY_axisDataByYearMonth(this, 1, yearmonth);
        Object[] income = ChartData.getY_axisDataByYearMonth(this, 2, yearmonth);

        Object[] property = ChartData.getY_axisDataByYearMonth(this, 3, yearmonth);
        Object[] surpuls = new Object[size];
        for (int i = 0; i < size; i++) {
            surpuls[i] = (Double) income[i] - (Double) expenditure[i];
        }

        AAChartModel aaChartModel = AAChartUtil.configureAAChartLineModel(title, subtitle, X_axis, income, expenditure, property, surpuls);
        /*图表视图对象调用图表模型对象,绘制最终图形*/
        AAChartView_line.aa_drawChartWithChartModel(aaChartModel);
//        });
    }

    void drawLineChartByYear(String title, String subtitle, String year) {
//        getUITaskDispatcher().asyncDispatch(() -> {
        //得到近年的月时间x轴标签
        String[] X_axis = DateUtils.getX_axisByYear(year);
        int size = X_axis.length;
        //数据转换
        Object[] expenditure = ChartData.getY_axisDataByYear(this, 1, year);
        Object[] income = ChartData.getY_axisDataByYear(this, 2, year);
        Object[] property = ChartData.getY_axisDataByYear(this, 3, year);
        Object[] surpuls = new Object[size];
        for (int i = 0; i < size; i++) {
            surpuls[i] = (Double) income[i] - (Double) expenditure[i];
        }


        AAChartModel aaChartModel = AAChartUtil.configureAAChartLineModel(title, subtitle, X_axis, income, expenditure, property, surpuls);
        /*图表视图对象调用图表模型对象,绘制最终图形*/
        AAChartView_line.aa_drawChartWithChartModel(aaChartModel);
//        });
    }

    void drawColumnChartPastSevenDaysData(String title, String subtitle) {
//        getUITaskDispatcher().asyncDispatch(() -> {
        //得到近七天的时间x轴标签
        String[] name = DateUtils.getPastSevenDate();

        //数据转换
        Object[] income = ChartData.getPastSevenDaysData(this, 2);
        Object[] expenditure = ChartData.getPastSevenDaysData(this, 1);
        Object[] property = ChartData.getPastSevenDaysData(this, 3);
        Object[] surpuls = new Object[7];
        for (int i = 0; i < 7; i++) {
            surpuls[i] = (Double) income[i] - (Double) expenditure[i];
        }

        AAChartModel aaChartModel = AAChartUtil.configureAAChartColumnModel(title, subtitle, name, income, expenditure, property, surpuls);
        /*图表视图对象调用图表模型对象,绘制最终图形*/
        AAChartView_column.aa_drawChartWithChartModel(aaChartModel);
//        });
    }

    void drawColumnChartByYearMonth(String title, String subtitle, String yearmonth) {
//        getUITaskDispatcher().asyncDispatch(() -> {
        //得到月的日时间x轴标签
        String[] X_axis = DateUtils.getX_axisByMonth(yearmonth);
        int size = X_axis.length;
        //数据转换
        Object[] expenditure = ChartData.getY_axisDataByYearMonth(this, 1, yearmonth);
        Object[] income = ChartData.getY_axisDataByYearMonth(this, 2, yearmonth);

        Object[] property = ChartData.getY_axisDataByYearMonth(this, 3, yearmonth);
        Object[] surpuls = new Object[size];
        for (int i = 0; i < size; i++) {
            surpuls[i] = (Double) income[i] - (Double) expenditure[i];
        }

        AAChartModel aaChartModel = AAChartUtil.configureAAChartColumnModel(title, subtitle, X_axis, income, expenditure, property, surpuls);
        /*图表视图对象调用图表模型对象,绘制最终图形*/
        AAChartView_column.aa_drawChartWithChartModel(aaChartModel);
//        });
    }

    void drawColumnChartByYear(String title, String subtitle, String year) {
//        getUITaskDispatcher().asyncDispatch(() -> {
        //得到近年的月时间x轴标签
        String[] X_axis = DateUtils.getX_axisByYear(year);
        int size = X_axis.length;
        //数据转换
        Object[] expenditure = ChartData.getY_axisDataByYear(this, 1, year);
        Object[] income = ChartData.getY_axisDataByYear(this, 2, year);
        Object[] property = ChartData.getY_axisDataByYear(this, 3, year);
        Object[] surpuls = new Object[size];
        for (int i = 0; i < size; i++) {
            surpuls[i] = (Double) income[i] - (Double) expenditure[i];
        }

        AAChartModel aaChartModel = AAChartUtil.configureAAChartColumnModel(title, subtitle, X_axis, income, expenditure, property, surpuls);
        /*图表视图对象调用图表模型对象,绘制最终图形*/
        AAChartView_column.aa_drawChartWithChartModel(aaChartModel);
//        });
    }

    void drawPieChart(String title, String subtitle, String commonDesc, List<ChartData> pieData) {
//        getUITaskDispatcher().asyncDispatch(() -> {
        //数据转换
        Object[][] transferData = ChartData.getTransferData(pieData);
        if (transferData == null) {
            MyToast.showMyToast(this, "数据为空！");
            HiLog.info(LABEL, "数据为空！");
            return;
        }
        AAChartModel aaChartModel = AAChartUtil.configurePieChart(title, subtitle, commonDesc, transferData);
        /*图表视图对象调用图表模型对象,绘制最终图形*/
        AAChartView_pie.aa_drawChartWithChartModel(aaChartModel);
//        });
    }

    public void showSelectYearList(Text text) {
        //布局加载
        Component parse = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_dialog_select_year, null, false);

        //取消按钮
        Button cancel = (Button) parse.findComponentById(ResourceTable.Id_cancel);
        //确定按钮
        Button ok = (Button) parse.findComponentById(ResourceTable.Id_ok);
        //选择器
        Picker picker = (Picker) parse.findComponentById(ResourceTable.Id_select_year_picker);
        ShapeElement shape = new ShapeElement();
        shape.setShape(ShapeElement.RECTANGLE);
        shape.setRgbColor(RgbColor.fromArgbInt(0xFF40E0D0));
        picker.setDisplayedLinesElements(shape, shape);
        //加载滑动数据
        String[] yearList = DateUtils.getSelectStatisticalYearList(5);
        picker.setMaxValue(yearList.length / 2);
        picker.setFormatter(i -> yearList[i]);

        picker.setSelectorItemNum(3);
        picker.setValue(yearList.length / 2);
        getUITaskDispatcher().asyncDispatch(() -> { //ui刷新线程显示

            //创建显示气窗
            CommonDialog dialog = new CommonDialog(this);
            dialog.setOffset(0, 10);
            dialog.setContentCustomComponent(parse);
            dialog.setSize(MATCH_CONTENT, MATCH_CONTENT);
            dialog.show();
            selectYearIndex = yearList.length / 2;
            picker.setValueChangedListener((picker1, oldi, newi) -> {
                selectYearIndex = newi;
            });
            cancel.setClickedListener(c -> {
                dialog.destroy();
                MyToast.showMyToast(this, "已取消");

            });
            ok.setClickedListener(c -> {
                text.setText(yearList[selectYearIndex]);
                statisticalTime=yearList[selectYearIndex];
                reloadYearData(yearList[selectYearIndex]);
                dialog.hide();

            });
        });
    }

    void reloadYearData(String time) {
        try {
            pieDateExpenditureByYear = ClassifyDBUtil.statisticalMoneyCurrentMonthOrYear(this, time, 1);
            pieDateIncomeByYear = ClassifyDBUtil.statisticalMoneyCurrentMonthOrYear(this, time, 2);
            pieDatepropertyByYear = ClassifyDBUtil.statisticalMoneyCurrentMonthOrYear(this, time, 3);
            yearReportFormsIncome = ClassifyDBUtil.statisticsReportByRtimeAndType1(this, time, 2);
            yearReportFormsExpenditure = ClassifyDBUtil.statisticsReportByRtimeAndType1(this, time, 1);
            yearReportFormsProperty = ClassifyDBUtil.statisticsReportByRtimeAndType1(this, time, 3);

        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        if (classifType == 0) {
            drawPieChart(time + "统计", "支出", "总计为", pieDateExpenditureByYear);
            reportFormProvider = new ReportFormProvider(yearReportFormsExpenditure, this);
        } else if (classifType == 1) {
            drawPieChart(time + "统计", "收入", "总计为", pieDateIncomeByYear);
            reportFormProvider = new ReportFormProvider(yearReportFormsIncome, this);
        } else {
            drawPieChart(time + "统计", "资产", "总计为", pieDatepropertyByYear);
            reportFormProvider = new ReportFormProvider(yearReportFormsProperty, this);
        }
        //直方图
        drawColumnChartByYear(time + "统计", "整体走势", time);
        //折线图
        drawLineChartByYear(time + "统计", "整体走势", time);
        //报表

    }

    public void showSelectYearAndMonthList(Text text) {
        //布局加载
        Component parse = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_dialog_select_year_month, null, false);

        //取消按钮
        Button cancel = (Button) parse.findComponentById(ResourceTable.Id_cancel);
        //确定按钮
        Button ok = (Button) parse.findComponentById(ResourceTable.Id_ok);
        //选择器
        Picker select_year = (Picker) parse.findComponentById(ResourceTable.Id_select_year_picker);
        Picker select_month = (Picker) parse.findComponentById(ResourceTable.Id_select_year_and_month_picker);
        ShapeElement shape = new ShapeElement();
        shape.setShape(ShapeElement.RECTANGLE);
        shape.setRgbColor(RgbColor.fromArgbInt(0xFF40E0D0));
        select_year.setDisplayedLinesElements(shape, shape);
        select_month.setDisplayedLinesElements(shape, shape);
        //加载滑动数据
        String[] yearList = DateUtils.getSelectStatisticalYearList(5);
        select_year.setMaxValue(yearList.length / 2);
        select_year.setFormatter(i -> yearList[i]);

        select_year.setSelectorItemNum(3);
        select_year.setValue(yearList.length / 2);

        String[] monthList = DateUtils.getSelectStatisticalMonthList();
        select_month.setMaxValue(monthList.length - 1);
        select_month.setFormatter(i -> monthList[i]);

        select_month.setSelectorItemNum(3);
        select_month.setValue(DateUtils.getCurrentMonth() - 1);


        getUITaskDispatcher().asyncDispatch(() -> { //ui刷新线程显示

            //创建显示气窗
            CommonDialog dialog = new CommonDialog(this);
            dialog.setOffset(0, 10);
            dialog.setContentCustomComponent(parse);
            dialog.setSize(MATCH_CONTENT, MATCH_CONTENT);
            dialog.show();
            selectYearIndex = yearList.length / 2;
            selectMonthIndex = DateUtils.getCurrentMonth() - 1;
            select_year.setValueChangedListener((picker1, oldi, newi) -> {
                selectYearIndex = newi;
            });

            select_month.setValueChangedListener((picker1, oldi, newi) -> {
                selectMonthIndex = newi;
            });

            cancel.setClickedListener(c -> {
                dialog.destroy();
                MyToast.showMyToast(this, "已取消");

            });
            ok.setClickedListener(c -> {
                String str = yearList[selectYearIndex] + "-" + monthList[selectMonthIndex];
                statisticalTime=str;
                reloadMonthData(str);
                text.setText(str);
                dialog.hide();

            });
        });
    }

    void reloadMonthData(String time) {
        try {
            pieDateExpenditureByMonth = ClassifyDBUtil.statisticalMoneyCurrentMonthOrYear(this, time, 1);
            pieDateIncomeByMonth = ClassifyDBUtil.statisticalMoneyCurrentMonthOrYear(this, time, 2);
            pieDatepropertyByMonth = ClassifyDBUtil.statisticalMoneyCurrentMonthOrYear(this, time, 3);
            monthReportFormsIncome = ClassifyDBUtil.statisticsReportByRtimeAndType1(this, time, 2);
            monthReportFormsExpenditure = ClassifyDBUtil.statisticsReportByRtimeAndType1(this, time, 1);
            monthReportFormsProperty = ClassifyDBUtil.statisticsReportByRtimeAndType1(this, time, 3);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        if (classifType == 0) {
            drawPieChart(time + "统计", "支出", "总计为", pieDateExpenditureByMonth);
            reportFormProvider = new ReportFormProvider(monthReportFormsExpenditure, this);
        } else if (classifType == 1) {
            drawPieChart(time + "统计", "收入", "总计为", pieDateIncomeByMonth);
            reportFormProvider = new ReportFormProvider(monthReportFormsIncome, this);
        } else {
            drawPieChart(time + "统计", "资产", "总计为", pieDatepropertyByMonth);
            reportFormProvider = new ReportFormProvider(monthReportFormsProperty, this);
        }
        //直方图
        drawColumnChartByYearMonth(time + "统计", "走势如下", time);
        //折线图
        drawLineChartByYearMonth(time + "统计", "走势如下", time);


    }


    public void showReportForms(AbilityContext context, Component component) {
        //性别类型
        getUITaskDispatcher().asyncDispatch(() -> {
            ListDialog listDialog = new ListDialog(context, ListDialog.SINGLE);
            listDialog.setCornerRadius(25);
            listDialog.getProvider();
            listDialog.setSize(900, 800);

            listDialog.setItems(new String[]{"1", "2"});
            listDialog.setProvider(reportFormProvider);
            listDialog.setButton(1, "确定", (iDialog, i) -> {
                iDialog.destroy();
            });
            //展示
            listDialog.show();
            listDialog.setOnSingleSelectListener(new IDialog.ClickedListener() {
                @Override
                public void onClick(IDialog iDialog, int i) {

                }
            });
        });
    }

    @Override
    public void onActive() {
        super.onActive();

    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
